package com.ruoyi.his.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.his.domain.*;
import com.ruoyi.his.domain.vo.OdsHospitalAllVo;
import com.ruoyi.his.domain.vo.OdsListHospitalAllVO;
import com.ruoyi.his.mapper.HisDepartmentMapper;
import com.ruoyi.his.mapper.OdsDoctorAllMapper;
import com.ruoyi.his.mapper.OdsDoctorDataMapper;
import com.ruoyi.his.mapper.*;
import com.ruoyi.his.service.IOdsDoctorDataService;
import com.ruoyi.system.domain.HisHospitalNation;
import com.ruoyi.system.domain.vo.HisHospitalNationVo;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.his.service.IOdsHospitalAllService;
import org.springframework.util.CollectionUtils;

/**
 * 国家机构Service业务层处理
 *
 * @author Alan
 * @date 2024-03-22
 */
@Service
public class OdsHospitalAllServiceImpl implements IOdsHospitalAllService
{
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private OdsHospitalAllMapper odsHospitalAllMapper;

    @Autowired
    private OdsDoctorAllMapper odsDoctorAllMapper;

    @Autowired
    private ISysUserService iUserService;

    @Autowired
    private HisDepartmentMapper hisDepartmentMapper;
    @Autowired
    private ReAreaMapper reAreaMapper;
    /**
     * 查询国家机构
     *
     * @param uniqueCode 国家机构主键
     * @return 国家机构
     */
    @Override
    public OdsHospitalAll selectOdsHospitalAllByUniqueCode(Integer uniqueCode)
    {
        return odsHospitalAllMapper.selectOdsHospitalAllByUniqueCode(uniqueCode);
    }

    /**
     * 查询国家机构列表
     *
     * @param odsHospitalAll 国家机构
     * @return 国家机构
     */
    @Override
    public List<OdsHospitalAll> selectOdsHospitalAllList(OdsHospitalAll odsHospitalAll)
    {
        List<OdsHospitalAll> odsHospitalAlls = odsHospitalAllMapper.selectOdsHospitalAllData(odsHospitalAll);
//        for (OdsHospitalAll nationVo:odsHospitalAlls){
//            if(org.apache.commons.lang3.StringUtils.isNotBlank(nationVo.getMedicalSpecialtiesCode())){
//                String[] split = nationVo.getMedicalSpecialtiesCode().split("/");
//                nationVo.setMedicalSpecialtiesValue(split.length);
//            }
//            final List<HisDepartment> hisDepartments = hisDepartmentMapper.selectList(new LambdaQueryWrapper<HisDepartment>()
//                    .eq(HisDepartment::getHospitalId, nationVo.getHospitalId()).eq(HisDepartment::getSignature, nationVo.getSignature()));
//            int total = 0;
//            for (HisDepartment department:hisDepartments){
//                if(org.apache.commons.lang3.StringUtils.isNotBlank(department.getDeviceId())){
//                    String[] split = department.getDeviceId().split(",");
//                    total += split.length;
//                }
//            }
//            nationVo.setMonitoringNumber(total);
//        }
        return odsHospitalAlls;
    }
    @Override
    public List<OdsHospitalAll> selectOdsHosAllInfo(OdsHospitalAll odsHospitalAll)
    {
        if(!StringUtils.isEmpty(odsHospitalAll.getRegisterNumber())){
            List<OdsHospitalAll> odsHospitalAlls = odsHospitalAllMapper.selectOdsHospitalAllListByReg(odsHospitalAll.getRegisterNumber());
            for (OdsHospitalAll nationVo:odsHospitalAlls){
                if(org.apache.commons.lang3.StringUtils.isNotBlank(nationVo.getMedicalSpecialtiesCode())){
                    String[] split = nationVo.getMedicalSpecialtiesCode().split("/");
                    nationVo.setMedicalSpecialtiesValue(split.length);
                }
                final List<HisDepartment> hisDepartments = hisDepartmentMapper.selectList(new LambdaQueryWrapper<HisDepartment>()
                        .eq(HisDepartment::getRegisterNumber, nationVo.getRegisterNumber()));
                int total = 0;
                for (HisDepartment department:hisDepartments){
                    if(org.apache.commons.lang3.StringUtils.isNotBlank(department.getDeviceId())){
                        String[] split = department.getDeviceId().split(",");
                        total += split.length;
                    }
                }
                nationVo.setMonitoringNumber(total);
            }
            return odsHospitalAlls;
        }
        if (!StringUtils.isEmpty(odsHospitalAll.getSignature()) && !StringUtils.isEmpty(odsHospitalAll.getHospitalId())){
            List<OdsHospitalAll>  odsHospitalAlls = odsHospitalAllMapper.selectBySigAndHosIdList(odsHospitalAll.getSignature(),odsHospitalAll.getHospitalId());
            for (OdsHospitalAll nationVo:odsHospitalAlls){
                if(org.apache.commons.lang3.StringUtils.isNotBlank(nationVo.getMedicalSpecialtiesCode())){
                    String[] split = nationVo.getMedicalSpecialtiesCode().split("/");
                    nationVo.setMedicalSpecialtiesValue(split.length);
                }
                final List<HisDepartment> hisDepartments = hisDepartmentMapper.selectList(new LambdaQueryWrapper<HisDepartment>()
                        .eq(HisDepartment::getRegisterNumber, nationVo.getRegisterNumber()));
                int total = 0;
                for (HisDepartment department:hisDepartments){
                    if(org.apache.commons.lang3.StringUtils.isNotBlank(department.getDeviceId())){
                        String[] split = department.getDeviceId().split(",");
                        total += split.length;
                    }
                }
                nationVo.setMonitoringNumber(total);
            }
            return odsHospitalAlls;
        }
        return new ArrayList<>();
    }

    /**
     * 新增国家机构
     *
     * @param odsHospitalAll 国家机构
     * @return 结果
     */
    @Override
    public int insertOdsHospitalAll(OdsHospitalAll odsHospitalAll)
    {
        return odsHospitalAllMapper.insertOdsHospitalAll(odsHospitalAll);
    }

    /**
     * 修改国家机构
     *
     * @param odsHospitalAll 国家机构
     * @return 结果
     */
    @Override
    public int updateOdsHospitalAll(OdsHospitalAll odsHospitalAll)
    {
        if (odsHospitalAll.getAreaCode()  != null ) {
            ReArea reArea = reAreaMapper.selectReAreaByCode(odsHospitalAll.getAreaCode());
            odsHospitalAll.setMedicalAreaname(reArea.getName());
        }
        /*odsHospitalAll.setRegionCode(reArea.getCode());
        odsHospitalAll.setRegionName(reArea.getName());*/
        return odsHospitalAllMapper.updateOdsHospitalAll(odsHospitalAll);
    }
    @Override
    public int updateOdsHospitalAllUpEnableds(OdsHospitalAll odsHospitalAll)
    {
        return odsHospitalAllMapper.updateOdsHospitalAll(odsHospitalAll);
    }

    /**
     * 批量删除国家机构
     *
     * @param uniqueCodes 需要删除的国家机构主键
     * @return 结果
     */
    @Override
    public int deleteOdsHospitalAllByUniqueCodes(Integer[] uniqueCodes)
    {
        return odsHospitalAllMapper.deleteOdsHospitalAllByUniqueCodes(uniqueCodes);
    }

    /**
     * 删除国家机构信息
     *
     * @param uniqueCode 国家机构主键
     * @return 结果
     */
    @Override
    public int deleteOdsHospitalAllByUniqueCode(Integer uniqueCode)
    {
        return odsHospitalAllMapper.deleteOdsHospitalAllByUniqueCode(uniqueCode);
    }

    public List<OdsHospitalAll> makeList(List<OdsHospitalAll> list,String signature){
        List<OdsHospitalAll> result = new ArrayList<>();
        for(OdsHospitalAll item:list){
            if(StringUtils.isNotEmpty(item.getHospitalId()) && StringUtils.isNotEmpty(item.getInstitutionName())) {
                item.setSignature(signature);
                item.setIntonum(1);
                item.setUpdateTime(new Date());
                result.add(item);
            }
        }
        return result;
    }

    /**
     * 对外的接口
     * @param vo
     * @return
     */
    @Override
    public AjaxResult insertOdsHosAllByHosId(OdsListHospitalAllVO vo) {
        SysDictData pa = new SysDictData();
        //查询是否有签名
        pa.setDictType("his");
        pa.setStatus("0");
        pa.setDictValue(vo.getSignature());
        int count = dictDataMapper.selectByCount(pa);
        if (count==0)
        {
            return AjaxResult.error("没有签名未授权访问！");
        }
        //设置签名
        List<OdsHospitalAll> list = makeList(vo.getData(),vo.getSignature());
        if(list.size()==0){
            return AjaxResult.error("请传入必填参数！");
        }
        //批量插入
        odsHospitalAllMapper.InsertOfList(list);
        //批量更新按名称
        odsHospitalAllMapper.updateOfListName(list);
        //批量更新按注册号
        odsHospitalAllMapper.updateOfListRegisterNumber(list);
        //批量更新按编号
        odsHospitalAllMapper.updateOfListHid(list);

        //批量删除按编号只保留最早一条
        odsHospitalAllMapper.deleteKeepOneByName();
        odsHospitalAllMapper.deleteKeepOneByIdRNumber();
        odsHospitalAllMapper.deleteKeepOneByHid();
        return AjaxResult.success();
    }
//    /**
//     * 对外的接口
//     * @param vo
//     * @return
//     */
//    @Override
//    public AjaxResult insertOdsHosAllByHosId(OdsListHospitalAllVO vo) {
//        SysDictData pa = new SysDictData();
//        //查询是否有签名
//        pa.setDictType("his");
//        pa.setStatus("0");
//        pa.setDictValue(vo.getSignature());
//        int count = dictDataMapper.selectByCount(pa);
//        if (count==0)
//        {
//            return AjaxResult.error("没有签名未授权访问！");
//        }
//        HashMap<String,OdsHospitalAll> map = makeMap(vo.getData());
//        OdsHospitalAll param = new OdsHospitalAll();
//        for(Map.Entry<String,OdsHospitalAll> item :map.entrySet()){
//            param.setHospitalId(null);
//            param.setInstitutionName(null);
//            param.setRegisterNumber(null);
//            OdsHospitalAll odsHospitalAll =  item.getValue();
//            //取到传递过来的机构登记号
//            String registerNumber = odsHospitalAll.getRegisterNumber();
//            odsHospitalAll.setSignature(vo.getSignature());
//            odsHospitalAll.setIntonum(1);
//            if (odsHospitalAll.getEnabled()==null ) {
//                odsHospitalAll.setEnabled(0);
//            }
//            List<OdsHospitalAll> result1 = null;
//            List<OdsHospitalAll> result2 = null;
//            List<OdsHospitalAll> result3 = null;
//
//
//
//            //根据注册号去更新hid
//            if(StringUtils.isNotEmpty(odsHospitalAll.getRegisterNumber())){
//                param.setRegisterNumber(odsHospitalAll.getRegisterNumber());
//                result1 =  odsHospitalAllMapper.selectByRegisterNumber(param);
//                for(OdsHospitalAll old:result1){
//                    if(old.getUniqueCode()!=null) {
//                        odsHospitalAll.setUpdateTime(new Date());
//                        odsHospitalAll.setUniqueCode(old.getUniqueCode());
//                        odsHospitalAllMapper.updateOdsHospitalAll(odsHospitalAll);
//                    }
//
//                }
//            }
//
//            //医院名称去更新hid
//            if(StringUtils.isNotEmpty(odsHospitalAll.getInstitutionName())){
//                param.setInstitutionName(odsHospitalAll.getInstitutionName());
//                result2 =  odsHospitalAllMapper.selectByName(param);
//                for(OdsHospitalAll old:result2){
//                    if(old.getUniqueCode()!=null) {
//                        odsHospitalAll.setUpdateTime(new Date());
//                        odsHospitalAll.setUniqueCode(old.getUniqueCode());
//                        odsHospitalAllMapper.updateOdsHospitalAll(odsHospitalAll);
//                    }
//
//                }
//            }
//
//            //医院编号去更新hid
//            if(StringUtils.isNotEmpty(odsHospitalAll.getHospitalId())){
//                param.setHospitalId(odsHospitalAll.getHospitalId());
//                result3 =  odsHospitalAllMapper.selectOdsHospitalAllList(param);
//                for(OdsHospitalAll old:result3){
//                    if(old.getUniqueCode()!=null) {
//                        odsHospitalAll.setUpdateTime(new Date());
//                        odsHospitalAll.setUniqueCode(old.getUniqueCode());
//                        odsHospitalAllMapper.updateOdsHospitalAll(odsHospitalAll);
//                        map.put(odsHospitalAll.getHospitalId(),null);
//                    }
//
//                }
//            }
//        }
//        for(Map.Entry<String,OdsHospitalAll> item :map.entrySet()){
//            OdsHospitalAll odsHospitalAll =  item.getValue();
//            if(odsHospitalAll!=null){
//                //没有在his系统中的医生作插入操作
//                odsHospitalAll.setCreateTime(new Date());
//                odsHospitalAll.setUniqueCode(null);
//                odsHospitalAllMapper.insertOdsHospitalAll(odsHospitalAll);
//            }
//        }
//        return AjaxResult.success();
//    }

    public String registerHospitalRule(OdsHospitalAll odsHospitalAll){
        SysDictData param = new SysDictData();
        //查询是否有签名
        param.setDictType("his");
        param.setStatus("0");
        param.setDictValue(odsHospitalAll.getSignature());
        int count = dictDataMapper.selectByCount(param);
        if (count==0)
        {
            return "没有签名未授权访问！";
        }
        //除去 为空或者是空字符的情况
        if (StringUtils.isEmpty(odsHospitalAll.getRegisterNumber())) {
            return "登记号不能为空！";
        }
        if (Objects.isNull(odsHospitalAll.getRegionCode())) {
            return "请选择辖区！";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getInstitutionName())) {
            return "机构名称不能为空";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getMedicalTelnum())) {
            return "机构联系方式不能为空！";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getMedicalLegalrepresentative())) {
            return "法人姓名不能为空！";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getMedicalLegalIdCard())) {
            return"法人身份证号不能为空！";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getMedicalChargerepresentative())) {
            return "主要负责人姓名不能为空！";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getMedicalChargeIdCard())) {
            return "主要负责人身份证不能为空！";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getLongitude())) {
            return "经度不能为空！";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getLatitude())) {
            return "纬度不能为空！";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getMedicalAddress())) {
            return "机构地址不能为空！";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getUsername())) {
            return "用户名不能为空！";
        }
        if (StringUtils.isEmpty(odsHospitalAll.getPassword())) {
            return "密码不能为空！";
        }
        return null;
    }
    /**
     * 注册的业务 选择医院的方式
     * @param odsHospitalAll
     * @return
     */
    @Override
    public AjaxResult registerHospitalSelect(OdsHospitalAll odsHospitalAll) {
        String msg = registerHospitalRule(odsHospitalAll);
        if(msg!=null){
            return AjaxResult.error(msg);
        }
        //取到传递过来的机构登记号
        String registerNumber = odsHospitalAll.getRegisterNumber();
        if(odsHospitalAll.getReCount()==0) {
            if (StringUtils.isEmpty(odsHospitalAll.getHospitalId())) {
                return AjaxResult.error("请选择机构！");
            }
        }
        OdsHospitalAll param = new OdsHospitalAll();
        //查询是否有签名
        param.setSignature(odsHospitalAll.getSignature());
        //查询在这个签名下his系统是否有这个机构
        param.setHospitalId(odsHospitalAll.getHospitalId());
//        List<OdsHospitalAll> result2 =  odsHospitalAllMapper.selectOdsHospitalAllList(param);
//        if (result2==null||result2.size()==0) {
//            return AjaxResult.error("未授权访问！");
//        }

        //判断用户是否存在
        String s = iUserService.checkUserNameUnique(odsHospitalAll.getUsername());
        if(!(s.equals(UserConstants.UNIQUE))){
            return AjaxResult.error("用户名已经存在！");
        }
        //通过登记号去判断user表中是否有该机构，如果有说明这家机构先注册了，http数据在后面接入
        //防止注册两次到user表中
        SysUser user = iUserService.selectUserByHosReg(registerNumber);
        if (!(user == null)) {
            return AjaxResult.error("该账号已经被注册！");
        }

        //密码加密
        String password = SecurityUtils.encryptPassword(odsHospitalAll.getPassword());
        SysUser sysUser = new SysUser();
        if (odsHospitalAll.getProvinceCode() != null && odsHospitalAll.getProvinceCode() != ""){
            sysUser.setProvinceCode(odsHospitalAll.getProvinceCode());
        }
        if (odsHospitalAll.getCityCode() != null && odsHospitalAll.getCityCode() != ""){
            sysUser.setCityCode(odsHospitalAll.getCityCode());
        }
        if (odsHospitalAll.getAreaCode() != null && odsHospitalAll.getAreaCode() != ""){
            sysUser.setAreaCode(odsHospitalAll.getAreaCode());
        }
        if (odsHospitalAll.getStreetCode() != null && odsHospitalAll.getStreetCode() != ""){
            sysUser.setStreetCode(odsHospitalAll.getStreetCode());
        }
        sysUser.setUserName(odsHospitalAll.getUsername());
        sysUser.setPassword(password);
        sysUser.setUserType("7");
        sysUser.setHospitalId(odsHospitalAll.getHospitalId());
        sysUser.setSignature(odsHospitalAll.getSignature());
        sysUser.setRegisterNumber(registerNumber);
        sysUser.setUpdateTime(new Date());
        odsHospitalAll.setIntonum(1);
        int j = iUserService.insertUser(sysUser);
        if(j>0){
            List<OdsHospitalAll> list = new ArrayList<>();
            list.add(odsHospitalAll);
            //批量插入
            odsHospitalAllMapper.InsertOfList(list);
            //批量更新按名称
            odsHospitalAllMapper.updateOfListName(list);
            //批量更新按注册号
            odsHospitalAllMapper.updateOfListRegisterNumber(list);
            //批量更新按编号
            odsHospitalAllMapper.updateOfListHid(list);

            //批量删除按编号只保留最早一条
            odsHospitalAllMapper.deleteKeepOneByName();
            odsHospitalAllMapper.deleteKeepOneByIdRNumber();
            odsHospitalAllMapper.deleteKeepOneByHid();
        }
        return AjaxResult.success();

    }
//    /**
//     * 注册的业务 选择医院的方式
//     * @param odsHospitalAll
//     * @return
//     */
//    @Override
//    public AjaxResult registerHospitalSelect(OdsHospitalAll odsHospitalAll) {
//        String msg = registerHospitalRule(odsHospitalAll);
//        if(msg!=null){
//            return AjaxResult.error(msg);
//        }
//        //取到传递过来的机构登记号
//        String registerNumber = odsHospitalAll.getRegisterNumber();
//        if (StringUtils.isEmpty(odsHospitalAll.getHospitalId())) {
//            return AjaxResult.error("请选择机构！");
//        }
//        OdsHospitalAll param = new OdsHospitalAll();
//        //查询是否有签名
//        param.setSignature(odsHospitalAll.getSignature());
//        //查询在这个签名下his系统是否有这个机构
//        param.setHospitalId(odsHospitalAll.getHospitalId());
//        List<OdsHospitalAll> result2 =  odsHospitalAllMapper.selectOdsHospitalAllList(param);
////        if (result2==null||result2.size()==0) {
////            return AjaxResult.error("未授权访问！");
////        }
//
//        //判断用户是否存在
//        String s = iUserService.checkUserNameUnique(odsHospitalAll.getUsername());
//        if(!(s.equals(UserConstants.UNIQUE))){
//            return AjaxResult.error("用户名已经存在！");
//        }
//        //通过登记号去判断user表中是否有该机构，如果有说明这家机构先注册了，http数据在后面接入
//        //防止注册两次到user表中
//        SysUser user = iUserService.selectUserByHosReg(registerNumber);
//        if (!(user == null)) {
//            return AjaxResult.error("该账号已经被注册！");
//        }
//
//        //密码加密
//        String password = SecurityUtils.encryptPassword(odsHospitalAll.getPassword());
//        SysUser sysUser = new SysUser();
//        sysUser.setUserName(odsHospitalAll.getUsername());
//        sysUser.setPassword(password);
//        sysUser.setUserType("7");
//        sysUser.setRegisterNumber(registerNumber);
//        sysUser.setUpdateTime(new Date());
//        odsHospitalAll.setIntonum(1);
//        int j = iUserService.insertUser(sysUser);
//        //更新当前临时注册的字段
//        int i = 0;
//        if(result2==null||result2.size()==0){
//            //没有在his系统中的医生作插入操作
//            i = odsHospitalAllMapper.insertOdsHospitalAll(odsHospitalAll);
//        }else {
//            //在his系统中的医生作更新操作
//            for(OdsHospitalAll item : result2){
//                if(item.getUniqueCode()!=null){
//                    odsHospitalAll.setUniqueCode(item.getUniqueCode());
//                    odsHospitalAllMapper.updateOdsHospitalAll(odsHospitalAll);
//                }
//
//            }
//        }
//        return AjaxResult.success();
//
//    }

    /**
     * 注册的业务 非选择医院的方式
     * @param odsHospitalAll
     * @return
     */
    @Override
    public AjaxResult registerHospitalUnSelect(OdsHospitalAll odsHospitalAll) {
        String msg = registerHospitalRule(odsHospitalAll);
        if(msg!=null){
            return AjaxResult.error(msg);
        }
        //取到传递过来的机构登记号
        String registerNumber = odsHospitalAll.getRegisterNumber();
        OdsHospitalAll param = new OdsHospitalAll();
        //查询是否有签名
        param.setSignature(odsHospitalAll.getSignature());
        //查询在这个签名下是否有这家名字的医院
        param.setInstitutionName(odsHospitalAll.getInstitutionName());
        List<OdsHospitalAll> result2 =  odsHospitalAllMapper.selectOdsHospitalAllList(param);

        //判断用户是否存在
        String s = iUserService.checkUserNameUnique(odsHospitalAll.getUsername());
        if(!(s.equals(UserConstants.UNIQUE))){
            return AjaxResult.error("用户名已经存在！");
        }
        //通过登记号去判断user表中是否有该机构，如果有说明这家机构先注册了，http数据在后面接入
        //防止注册两次到user表中
        SysUser user = iUserService.selectUserByHosReg(registerNumber);
        if (!(user == null)) {
            return AjaxResult.error("该账号已经被注册！");
        }

        //密码加密
        String password = SecurityUtils.encryptPassword(odsHospitalAll.getPassword());
        SysUser sysUser = new SysUser();
        sysUser.setUserName(odsHospitalAll.getUsername());
        sysUser.setPassword(password);
        sysUser.setUserType("7");
        sysUser.setRegisterNumber(registerNumber);
        sysUser.setUpdateTime(new Date());
        odsHospitalAll.setIntonum(1);
        int j = iUserService.insertUser(sysUser);
        //更新当前临时注册的字段
        int i = 0;
        if(result2==null||result2.size()==0){
            //没有在his系统中的医生作插入操作
            i = odsHospitalAllMapper.insertOdsHospitalAll(odsHospitalAll);
        }else {
            for(OdsHospitalAll item : result2){
                if(item.getUniqueCode()!=null){
                    odsHospitalAll.setUniqueCode(item.getUniqueCode());
                    odsHospitalAllMapper.updateOdsHospitalAll(odsHospitalAll);
                }

            }
        }
        return AjaxResult.success();
//        if (i != 0&&j != 0) {
//            //老数据
//            List <OdsHospitalAll> olist =   odsHospitalAllMapper.selectBySignatureIsNull(registerNumber);
//            //新数据
//            OdsHospitalAll ndata =  odsHospitalAllMapper.selectBySignatureIsNotNull(registerNumber);
//            for(OdsHospitalAll olddata:olist){
//                //根据注册号，更新老数据
//                odsHospitalAll.setUniqueCode(olddata.getUniqueCode());
//                odsHospitalAllMapper.updateOdsHospitalAll(odsHospitalAll);
//            }
//            if(olist.size()>0) {
//                //更新老数据完成后把这条新数据删除
//                odsHospitalAllMapper.deleteOdsHospitalAllByUniqueCode(ndata.getUniqueCode());
//            }
//            return AjaxResult.success();
//        } else {
//            if(i != 0&& j == 0){
//                OdsHospitalAll ndata =  odsHospitalAllMapper.selectBySignatureIsNotNull(registerNumber);
//                int delete = odsHospitalAllMapper.deleteOdsHospitalAllByUniqueCode(ndata.getUniqueCode());
//            }else {
//                int del =iUserService.delByHosReg(sysUser.getRegisterNumber());
//            }
//            return AjaxResult.error("系统繁忙，稍后再试！");
//        }

    }
    @Override
    public String importData(List<OdsHospitalAllVo> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入机构数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (OdsHospitalAllVo user : userList)
        {
            try
            {
                if(org.apache.commons.lang3.StringUtils.isBlank(user.getRegisterNumber())){
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、登记号 " + user.getRegisterNumber() + " 导入失败：";
                    failureMsg.append(msg );
                    break;
                }
                // 验证是否存在这个用户
                OdsHospitalAll u = odsHospitalAllMapper.selectRegisterNumber(user.getRegisterNumber());
                OdsHospitalAll odsHospitalAll= new OdsHospitalAll();
                BeanUtils.copyProperties(user,odsHospitalAll);
                if (StringUtils.isNull(u))
                {
                    int i = this.odsHospitalAllMapper.insertOdsHospitalAll(odsHospitalAll);
                    //根据机构名称匹配医生表   设置登记号
//                    final List<OdsDoctorAll> odsDoctorAlls = odsDoctorAllMapper.selectList(new LambdaQueryWrapper<OdsDoctorAll>()
//                            .eq(OdsDoctorAll::getUnitname, user.getInstitutionName()));
//                    for (OdsDoctorAll doctorAll:odsDoctorAlls){
//                        doctorAll.setRegisterNumber(user.getRegisterNumber());
//                        odsDoctorAllMapper.updateOdsDoctorAllByIdcard(doctorAll);
//                    }
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、登记号 " + user.getRegisterNumber() + " 导入成功");
                }
                else
                {
                    odsHospitalAll.setUniqueCode(u.getUniqueCode());
                    int i1 = this.updateOdsHospitalAll(odsHospitalAll);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、登记号 " + user.getRegisterNumber() + " 更新成功");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getRegisterNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public OdsHospitalAll getByRegisterNumberAndSignature(String registerNumber,String signature) {
        OdsHospitalAll odsHospitalAll = new OdsHospitalAll();
        odsHospitalAll.setRegisterNumber(registerNumber);
        odsHospitalAll.setSignature(signature);
        List<OdsHospitalAll> odsHospitalAlls = odsHospitalAllMapper.selectOdsHospitalAllList(odsHospitalAll);
        if (CollectionUtils.isEmpty(odsHospitalAlls)){
            return null;
        }else {
            return odsHospitalAlls.get(0);
        }
    }

    @Override
    public OdsHospitalAll getByHospitalIdAndSignature(String hospitalId,String signature) {
        OdsHospitalAll odsHospitalAll = new OdsHospitalAll();
        odsHospitalAll.setHospitalId(hospitalId);
        odsHospitalAll.setSignature(signature);
        List<OdsHospitalAll> odsHospitalAlls = odsHospitalAllMapper.selectOdsHospitalAllList(odsHospitalAll);
        if (CollectionUtils.isEmpty(odsHospitalAlls)){
            return null;
        }else {
            return odsHospitalAlls.get(0);
        }
    }
    @Override
    public List<OdsHospitalAll> selectOdsHosListBySig(String signature) {

        return odsHospitalAllMapper.selectListBySig(signature);

    }

    @Override
    public Integer checkSameName(String signature) {
        OdsHospitalAll parameter = new OdsHospitalAll();
        parameter.setSignature(signature);
        List<OdsHospitalAll> list1 = odsHospitalAllMapper.selectListBySig(signature);
        if(list1==null || list1.size()==0){
            return 1;
        }else {
            List<OdsHospitalAll> list = odsHospitalAllMapper.checkSameName(signature);
            if(list==null || list.size()==0){
                return 0;
            }else{
                return 1;
            }
        }
    }
    @Override
    public OdsHospitalAll selectRegisterNumber(String uniqueCode) {
        return odsHospitalAllMapper.selectRegisterNumber(uniqueCode.toString());
    }
}
